/**
 * Created by 37500 on 2016/10/27.
 */

var gulp = require('gulp');
//css
var compass = require('gulp-compass');
var cleanCss = require('gulp-clean-css');
//rename
var rename = require('gulp-rename');
//tool
var babel = require('gulp-babel');
var plumber = require('gulp-plumber');

//js
var uglify = require('gulp-uglify');
var concat = require('gulp-concat');

//seaJs
var seajsCombo  = require('gulp-seajs-combo');

// sass compile
gulp.task('compass', function() {
    gulp.src('sass/main.scss')
        .pipe(plumber())
        .pipe(compass({
            css: "dist/css",
            sass: "sass"
        }))
        .pipe(gulp.dest('dist/css/'))
        .pipe(cleanCss())
        .pipe(rename({
            extname: '\.min.css'
        }))
        .pipe(gulp.dest('dist/css/'));
});

// js compile
gulp.task('jsUglify', ['jsCompile'], function() {
    gulp.src('./dist/js/main.js')
        .pipe(plumber())
        .pipe(uglify())
        .pipe(rename({
            extname: '\.min.js'
        }))
        .pipe(gulp.dest('./dist/js/'));
});

// compile es6
gulp.task('jsCompile', function() {
    return gulp.src(['js/tools.js', 'js/main.js'])
        .pipe(concat({
            path: 'main.js'
        }))
        .pipe(plumber())
        .pipe(babel())
        .pipe(gulp.dest('./dist/js/'));
});


// es6,es7 test
gulp.task('jsTest', function(){
    return gulp.src(['js/ejs6/*.js'])
        .pipe(plumber())
        .pipe(babel())
        .pipe(gulp.dest('./dist/js/ejs6/'))
});

//sea_0. lib文件加载

gulp.task('jslib', function(){
    return gulp.src('js/lib/*.js')
        .pipe(gulp.dest('dist/js/lib/'))
});

//sea_1.首先编译js
gulp.task('jsSetep', function(){
     return gulp.src('js/mod/*.js')
        .pipe(plumber())
        .pipe(babel())
        .pipe(gulp.dest('./dist/js/mod/'));
});

//sea_2.把mod编译
gulp.task('seaJs', ['jsSetep'], function(){
    gulp.src('js/mod/mod_*.js')
    .pipe(plumber())
    .pipe(seajsCombo())
    .pipe(rename({
        extname: '\.pb.js'
    }))
    .pipe(gulp.dest('dist/js/mod/')); //输出到目录
});

gulp.task('default', ['jslib'], function() {
    gulp.watch('sass/*.scss', ['compass']);
    //gulp.watch('js/*.js', ['jsUglify']);
    gulp.watch('js/ejs6/*.js', ['jsTest']);
    gulp.watch('js/mod/*.js', ['seaJs']);
});